package models

import (
	"fmt"
	"gorm.io/gorm"
	"gorm.io/gorm/clause"
)

// 发送任务 和机器的关系
// 1对多 机器只能属于1个发送任务
// 新建发送任务 就需要填入ip
// 主配置文件池
// job 对象 机器：ip+port
//
//	2 k8s 的服务发现：集群外部发送
type MonitorOndutyChange struct {
	Model // 不用每次写ID 和 createAt了
	// D:\nyy_work\go_path\src\github.com\prometheus\alertmanager\config\config.go
	OndutyGroupId uint
	UserID        uint
	// 公共的
	DateString   string `json:"dateString" gorm:"comment:计划哪一天"`
	OnDutyUserId uint   `json:"onDutyUserId" gorm:"comment:谁值班了"`
	OriginUserId uint   `json:"originUserId" gorm:"comment:原来是谁"`

	// 要求前端请求 是2个username
	TargetUserName string `json:"targetUserName" gorm:"-"`
	OriginUserName string `json:"originUserName"  gorm:"-"`

	Key            string `json:"key" gorm:"-"`      //给前端表格
	PoolName       string `json:"poolName" gorm:"-"` //给前端表格
	CreateUserName string `json:"createUserName" gorm:"-"`
}

func (obj *MonitorOndutyChange) Create() error {
	return Db.Create(obj).Error

}
func (obj *MonitorOndutyChange) DeleteOne() error {

	return Db.Select(clause.Associations).Unscoped().Delete(obj).Error

}

func (obj *MonitorOndutyChange) CreateOne() error {
	return Db.Create(obj).Error

}

func (obj *MonitorOndutyChange) UpdateOne() error {
	return Db.Debug().Where("id = ?", obj.ID).Updates(obj).Error

}

func (obj *MonitorOndutyChange) FillFrontAllData() {

	//
	//dbAlertI, _ := GetMonitorAlertmanagerInstanceById(int(obj.AlertManagerInstanceId))
	//if dbAlertI != nil {
	//	obj.PoolName = dbPoll.Name
	//}

	obj.Key = fmt.Sprintf("%d", obj.ID)

}
func GetMonitorOndutyChangeAll() (objs []*MonitorOndutyChange, err error) {
	err = Db.Find(&objs).Error
	return
}

func GetMonitorOndutyChangeCount() int {
	var count int64
	Db.Table("form_designs").Count(&count)
	return int(count)
}

func GetMonitorOndutyChangeByOnDutyUserIdAndTimeRange(onDutyUserId int, startDay, endDay string) (objs []*MonitorOndutyChange, err error) {
	err = Db.Debug().Where("onduty_group_id = ? and created_at  >= ? and created_at <= ? ", onDutyUserId, startDay, endDay).Find(&objs).Error
	return
}

func GetMonitorOndutyChangeByOndutyGroupIdAndDay(ondutyGroupId uint, dateString string) (obj *MonitorOndutyChange, err error) {
	err = Db.Where("onduty_group_id = ? and date_string  = ? ", ondutyGroupId, dateString).First(&obj).Error
	return
}

// select * from ecs where
func GetMonitorOndutyChangeByIdsWithLimitOffset(ids []int, limit, offset int) (objs []*MonitorOndutyChange, err error) {
	err = Db.Limit(limit).Offset(offset).Find(&objs, ids).Error
	return
}

// 查询这个节点池管理的发送Job
func GetMonitorOndutyChangeByPoolId(poolId uint) (objs []*MonitorOndutyChange, err error) {

	//err = Db.Where("enable=1 and pool_id = ?", poolId).Find(&objs).Error
	err = Db.Where(" pool_id = ?", poolId).Find(&objs).Error
	return

}

func GetMonitorOndutyChangeById(id int) (*MonitorOndutyChange, error) {

	var dbObj MonitorOndutyChange
	err := Db.Where("id = ?", id).First(&dbObj).Error
	if err != nil {
		if err == gorm.ErrRecordNotFound {
			return nil, fmt.Errorf("MonitorOndutyChange不存在")
		}
		return nil, fmt.Errorf("数据库错误:%w", err)
	}
	return &dbObj, nil

}
